BablFishReference is coming to life
authorØyvind Kolås <ok@src.gnome.org>
Mon, 15 Aug 2005 14:42:07 +0000 (14:42 +0000)
committerØyvind Kolås <ok@src.gnome.org>
Mon, 15 Aug 2005 14:42:07 +0000 (14:42 +0000)
ChangeLog
babl/babl-classes.h
babl/babl-fish.c
babl/babl-ids.h
babl/babl-image.c
babl/babl-image.h
babl/babl-type.c
tests/grayscale_to_rgb.c

index 5e41083ec99ffebf519961ee576204e0da0e2f9a..bde9a00d026422336b9f4b4bdba45fbac7c55089 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2005-08-15  Øyvind Kolås  <pippin@gimp.org>
+       
+       * babl/babl-classes.h: BablPixelFormat: changed order of attributes to
+       match BablModel. BablFish: removed **from and **to lists, since they
+       were added at a wrong offset from the instance start anyways.
+       * babl/babl-fish.c: Made the reference fish work for linear buffers
+       with constant datatype
+       * babl/babl-ids.h: added BABL_RGBA_DOUBLE
+       * babl/babl-image.[ch]: made BablImage be allocated in a single chunk,
+       added babl_image_new_from_linear()
+       * babl/babl-type.c: added assertion about bits%8==0
+       * babl/babl-base/babl-base-conversions-model.c: made BABL_PLANAR_SANITY accept
+       single band buffers. added conversions for premultiplied grayscale.
+       * babl/babl-base/babl-base-pixel-formats.c: added "rgba-double"
+
 2005-08-15  Øyvind Kolås  <pippin@gimp.org>
 
        * Makefile-mini: compile both the nop and introspect programs by
index 4055fc9597d06378af32ea8ffdb5f141d160179f..a4f0786d759d8fb79d95bd5ddf5a7a5d1c41dd8f 100644 (file)
@@ -169,8 +169,8 @@ typedef struct
   BablInstance     instance;
   BablConversion **from; /*< NULL terminated list of conversions from class */
   BablConversion **to;   /*< NULL terminated list of conversions to class   */
-  int              planar;
   int              bands;
+  int              planar;
   BablModel      **model;
   BablComponent  **component;
   BablType       **type;
@@ -197,9 +197,6 @@ typedef struct
 typedef struct
 {
   BablFish         fish;
-  BablConversion **from; /*< these are here for a later stage, when calculated*/
-  BablConversion **to;   /*< reference conversions can be used for "segment"  */
-                         /*< conversions where no other conversions exist.    */
   BablConversion  *type_to_double;
   BablConversion  *model_to_rgba;
   BablConversion  *rgba_to_model;
index 938e8c2f9b14f579d8149cdea7c00e6d38063867..0e376afd39925efbbf31af9ee6123d61ded54948 100644 (file)
@@ -25,6 +25,8 @@
 
 #include "babl-type.h"
 #include "babl-model.h"
+#include "babl-image.h"
+#include "babl-pixel-format.h"
 
 static int 
 each_babl_fish_destroy (Babl *babl,
@@ -185,6 +187,8 @@ void *fooA;
 void *fooB;
 void *fooC;
 
+#define BABL_MAX_BANDS   32
+
 int
 babl_fish_process (BablFish *babl_fish,
                    void     *source,
@@ -192,10 +196,12 @@ babl_fish_process (BablFish *babl_fish,
                    int       n)
 {
   Babl *babl;
+  BablImage *imageA;
+  BablImage *imageB;
+  BablImage *imageC;
 
-  fooA = malloc(1000); 
-  fooB = malloc(1000); 
-  fooC = malloc(1000); 
+  fooA = babl_malloc(sizeof (double) * n * 4); 
+  fooB = babl_malloc(sizeof (double) * n * 4); 
 
   assert (source);
   assert (destination);
@@ -214,15 +220,50 @@ babl_fish_process (BablFish *babl_fish,
           fooA,
           n*  ((BablPixelFormat*)(babl_fish->source))->bands
           );
+
   /* calculate planar representation of fooA, and fooB */
+
+  imageA = babl_image_new_from_linear (fooA,
+      (Babl*) ((BablPixelFormat*) babl->fish.source)->model[0]);
+  imageB = babl_image_new_from_linear (fooB, (Babl*)babl_model_id (BABL_RGBA));
   /* transform fooA into fooB fooB is rgba double */
+
+  ((BablConversion*)(babl->reference_fish.model_to_rgba))->function.planar(
+          imageA->bands, 
+          imageA->data,
+          imageA->pitch,
+          imageB->bands, 
+          imageB->data,
+          imageB->pitch,
+          n);
+  babl_free (imageA);
+  babl_free (imageB);
+
   /* calculate planar representation of fooC */
   /* transform fooB into fooC fooC is ???? double */
 
+  imageB = babl_image_new_from_linear (fooB, (Babl*)babl_model_id (BABL_RGBA));
+  imageC = babl_image_new_from_linear (fooA, (Babl*)((BablPixelFormat*)babl->fish.destination)->model[0]);
+
+  ((BablConversion*)(babl->reference_fish.rgba_to_model))->function.planar(
+          imageB->bands, 
+          imageB->data,
+          imageB->pitch,
+          imageC->bands, 
+          imageC->data,
+          imageC->pitch,
+          n);
+
+
   ((BablConversion*)(babl->reference_fish.double_to_type))->function.linear(
           fooA, destination, n * ((BablPixelFormat*)(babl_fish->destination))->bands
           );
 
+  babl_free (imageB);
+  babl_free (imageC);
+
+  babl_free (fooA);
+  babl_free (fooB);
   return 0;
 }
 
index 2b284e36a0a20382e1b4b5209364256d250259fc..84656b33b90910f79e5de0836ed234dea1950441 100644 (file)
@@ -78,6 +78,7 @@ enum {
   BABL_SRGBA,
   BABL_RGB_FLOAT,
   BABL_RGBA_FLOAT,
+  BABL_RGBA_DOUBLE,
   BABL_CMYK_FLOAT,
   BABL_CMYKA_FLOAT,
   BABL_YUV420,
index 6f398c7a5862d13eca12406b44d49bb62aa44221..48f470d78ef06d1f5f1389a8e796da525061ddeb 100644 (file)
@@ -34,9 +34,6 @@ static int
 each_babl_image_destroy (Babl *babl,
                          void *data)
 {
-  babl_free (babl->image.component);
-  babl_free (babl->image.pitch);
-  babl_free (babl->image.stride);
   babl_free (babl);
 
   return 0;  /* continue iterating */
@@ -53,7 +50,18 @@ image_new (int             bands,
   Babl *self;
   int        band;
 
-  self                = babl_calloc (sizeof (BablImage), 1);
+  self                = babl_calloc (
+                           sizeof (BablImage) +
+                           sizeof (BablComponent*) * (bands+1) +
+                           sizeof (void*)          * (bands+1) +
+                           sizeof (int)            * (bands+1) +
+                           sizeof (int)            * (bands+1),1);
+
+  self->image.component     = ((void *)self)                  + sizeof (BablImage);
+  self->image.data          = ((void *)self->image.component) + sizeof (BablComponent*) * (bands+1);
+  self->image.pitch         = ((void *)self->image.data)      + sizeof (void*)          * (bands+1);
+  self->image.stride        = ((void *)self->image.pitch)     + sizeof (int)            * (bands+1);
+/*self->image.foo           = ((void *)self->image.stride)    + sizeof (int)            * (bands+1);*/
 
   self->class_type    = BABL_IMAGE;
   self->instance.id   = 0;
@@ -61,11 +69,6 @@ image_new (int             bands,
 
   self->image.bands         = bands;
 
-  self->image.component     = babl_malloc (sizeof (BablComponent*) * (bands+1));
-  self->image.data          = babl_malloc (sizeof (void*)          * (bands+1));
-  self->image.pitch         = babl_malloc (sizeof (int)            * (bands+1));
-  self->image.stride        = babl_malloc (sizeof (int)            * (bands+1));
-
   for (band=0; band < bands; band++)
     {
       self->image.component[band] = component[band];
@@ -81,6 +84,66 @@ image_new (int             bands,
   return (BablImage*) self;
 }
 
+BablImage *
+babl_image_new_from_linear (void  *buffer,
+                            Babl  *babl)
+{
+  BablImage     *self;
+  int            band;
+  BablComponent *component [BABL_MAX_BANDS];
+  void          *data      [BABL_MAX_BANDS];
+  int            pitch     [BABL_MAX_BANDS];
+  int            stride    [BABL_MAX_BANDS];
+
+  int            offset=0;
+  int            calc_pitch=0;
+  switch (babl->class_type)
+    {
+      case BABL_PIXEL_FORMAT:
+        for (band=0; band < babl->pixel_format.bands; band++)
+          {
+            BablType *type = babl->pixel_format.type[band];
+            calc_pitch += (type->bits / 8);
+          }
+
+        for (band=0; band < babl->pixel_format.bands; band++)
+          {
+            BablType *type = babl->pixel_format.type[band];
+
+            component[band] = babl->pixel_format.component[band];
+            data[band]      = buffer + offset;
+            pitch[band]     = calc_pitch;
+            stride[band]    = 0;
+            
+            offset += (type->bits / 8);
+          }
+        break;
+      case BABL_MODEL:
+        for (band=0; band < babl->model.components; band++)
+          {
+            calc_pitch += (64 / 8);
+          }
+
+        for (band=0; band < babl->model.components; band++)
+          {
+            component[band] = babl->model.component[band];
+            data[band]      = buffer + offset;
+            pitch[band]     = calc_pitch;
+            stride[band]    = 0;
+            
+            offset += (64 / 8);
+          }
+        break;
+      default:
+        babl_log ("%s(): Eeek!", __FUNCTION__);
+        break;
+    }
+
+  self = image_new (babl->model.components, component, data, pitch, stride);
+  return self;
+}
+
 BablImage *
 babl_image_new (void *first,
                 ...)
@@ -148,7 +211,7 @@ babl_image_new (void *first,
 
 void
 babl_image_each (BablEachFunction  each_fun,
-                    void             *user_data)
+                 void             *user_data)
 {
   int i;
   return;
index db4a0c59a34b35a0db75ee69bd8de4ad3b37e2b1..50b351cfc4ad3cef1ebc8529553492679b77955e 100644 (file)
@@ -29,4 +29,11 @@ void        babl_image_each       (BablEachFunction  each_fun,
 void        babl_image_destroy    (void);    
 BablImage * babl_image_new        (void *first_component,
                                    ...);
+
+/* create a new BablImage based on a packed BablPixelFormat (or BablModel assumed to
+ * use only doubles).
+ */
+BablImage *
+babl_image_new_from_linear (void *buffer,
+                            Babl *format);
 #endif
index 211733d9236f42f9a883de3d1eb8c7fad17d8860..4db95bddbc1ade1d53a222484bc451ee3401c9ff 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+#include <string.h>
+#include <stdarg.h>
+#include <assert.h>
+
 #include "babl-internal.h"
 #include "babl-db.h"
 
-#include <string.h>
-#include <stdarg.h>
 
 static int 
 each_babl_type_destroy (Babl *babl,
@@ -42,6 +44,9 @@ type_new (const char  *name,
 {
   Babl *self;
 
+  assert (bits != 0);
+  assert (bits % 8 == 0);
+  
   self                = babl_calloc (sizeof (BablType), 1);
   self->class_type    = BABL_TYPE;
   self->instance.id   = id;
index 7d3e717d2b61e193e4d4a9e93d1406d585ff5d96..deb9cacbb88f3c2b7665c968c64879abd0906869 100644 (file)
@@ -53,7 +53,8 @@ test (void)
       babl_component ("green"),
       babl_component ("blue"),
       NULL
-    ));
+    )
+  );
 
   babl_fish_process (fish, 
      grayscale_buf, rgb_buf,